﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Rechargeable.Amazon.Localization;
using Rechargeable.Amazon.Permissions;
using System.Threading.Tasks;
using Volo.Abp.UI.Navigation;

namespace Rechargeable.Amazon.Web.Menus;

public class AmazonMenuContributor : IMenuContributor
{
    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        if (context.Menu.Name == StandardMenus.Main)
        {
            await ConfigureMainMenuAsync(context);
        }
    }

    private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
    {
        var l = context.ServiceProvider.GetRequiredService<IStringLocalizer<AmazonResource>>();
        var authorizationService = context.ServiceProvider.GetRequiredService<IAuthorizationService>();

        if (await authorizationService.IsGrantedAnyAsync(AmazonPermissions.GetAll()) == false)
        {
            return;
        }

        //Add main menu items.
        context.Menu.AddItem(new ApplicationMenuItem(AmazonMenus.Prefix, displayName: l["Amazon"], "", icon: "fa fa-globe")
            .AddItem(new ApplicationMenuItem(AmazonMenus.Prefix, displayName: l["Amazon:Product"], "/Amazon", icon: "fa fa-circle-thin ", requiredPermissionName: AmazonPermissions.Products))
            ) ;
        await Task.CompletedTask;
    }
}
